package io.renren.wh.merchant.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.wh.merchant.dao.RateDao;
import io.renren.wh.merchant.entity.RateChannelEntity;
import io.renren.wh.merchant.entity.RateEntity;
import io.renren.wh.merchant.service.RateService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("rateService")
public class RateServiceImpl extends ServiceImpl<RateDao, RateEntity> implements RateService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<RateEntity> queryWrapper = new QueryWrapper<>();
        String interfaceId = (String) params.get("interfaceId");
        if (!StringUtils.isEmpty(interfaceId)) {
            queryWrapper.eq("interfaceId",interfaceId);
        }
        String merchantname = (String) params.get("merchantname");
        if (!StringUtils.isEmpty(merchantname)) {
            queryWrapper.like("merchantname",merchantname);
        }
        IPage<RateEntity> page = this.page(
                new Query<RateEntity>().getPage(params),
                queryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public void insertRateChannel(List<RateChannelEntity> list) {
        baseMapper.insertRateChannel(list);
    }

    @Override
    public List<Integer> queryChannelByRateId(Integer rateId) {
        return baseMapper.queryChannelByRateId(rateId);
    }

    @Override
    public RateEntity queryByMerchantidAndInterfacecode(Integer merchantId, Integer interfaceCode) {
        QueryWrapper<RateEntity> queryWrapper = new QueryWrapper<>();
        if (null == merchantId || null == interfaceCode){
            return null;
        }
        queryWrapper.eq("merchantid",merchantId);
        queryWrapper.eq("interfacecode",interfaceCode);
        return super.getOne(queryWrapper);
    }

    @Override
    public List<RateEntity> queryByMerchantId(Integer merchantId) {

        List<RateEntity> rateEntityList = this.baseMapper.selectList(new QueryWrapper<RateEntity>().eq("merchantId", merchantId));

        return rateEntityList;
    }

}
